// pages/addGoods/addGoods.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    "money": 0,
    "btnText":"加入购物车",
    "addEnable":false,
    "number":1,
    "images":[
      "https://c-ssl.dtstatic.com/uploads/item/201110/21/20111021192716_Ffuxa.thumb.1000_0.jpg"
    ],
    "propertyValue":{},
    "good":{
      "sell_time_status": 0,
      "id": 68339,
      "pack_cost": 16,
      "sales": 44429,
      "cover_img": "",
      "property": [{
        "is_open_checkbox": false,
        "id": 190,
        "values": [{
          "is_default": 0,
          "id": 582,
          "code": "Z5EA54C3D0E4279185",
          "value": "标准冰"
        }, {
          "is_default": 0,
          "id": 583,
          "code": "Z5EA54C3D0E9682750",
          "value": "去冰"
        }],
        "name": "温度",
        "desc": null
      }],
      "is_sell": true,
      "goods_type": 2,
      "entity": [{
        "spec_id": "",
        "trade_mark": "1010010025",
        "id": "72cf1279c0a422ce",
        "stock": "9999817.00",
        "spec_text": [],
        "spec": [],
        "image": "s23107/2020/04/26/559a075d81060b23c7.jpg",
        "num": 1,
        "price": 30,
        "membership_price": 0
      }],
      "stall_code": "",
      "sort": 0,
      "price": 18,
      "unit": "件",
      "imageArr": [
        "https://img-shop.qmimg.cn/s23107/2020/04/26/559a075d81060b23c7.jpg?imageView2/2/w/600/h/600"
      ],
      "membership_price": 0,
      "use_property": 1,
      "unit_type": 0,
      "min_buy_num": 0,
      "specs": [],
      "content": "饱满大颗的新鲜杨梅,满杯手工去核,搭配茉莉初雪茶底,清爽多汁",
      "use_spec": false,
      "stock": "9999817.00",
      "type": 1,
      "is_label": 0,
      "name": "霸气杨梅",
      "images": "https://img-shop.qmimg.cn/s23107/2020/04/26/559a075d81060b23c7.jpg?imageView2/2/w/400/h/400"
    }
  },

  changeDefault:function(e){
    const index = e.currentTarget.dataset['index']
    const itemIndex = e.currentTarget.dataset['itemindex']
    let tempGood = this.data.good;
    tempGood.property[index].values.forEach(value => value.is_default = 0)
    tempGood.property[index].values[itemIndex].is_default = 1

    let propertyValue = tempGood.property[index].values[itemIndex]

    let enable = 0;
    tempGood.property.forEach(element => {
      let newList = element.values.filter(value=> value.is_default == 1)
      if(newList.length > 0){
        enable = 1
        return true;
      }
    })

    const mount = this.data.number * this.data.good.price
    this.setData({
      good: tempGood,
      addEnable: enable == 1,
      btnText: mount > 0 ? "加入购物车￥" + mount : "加入购物车",
      propertyValue:{"id": propertyValue.id,"value":propertyValue.value}
    })
  },

  handlePropertyReduce() {
    if(this.data.number === 1) return
    let count = this.data.number
    count--
    let mount = count * this.data.good.price
    this.setData({
      number : count,
      money: mount,
      btnText: mount > 0 ? "加入购物车￥" + mount : "加入购物车"
    })
  },
  handlePropertyAdd() {
    let count = this.data.number
    count++
    let mount = count * this.data.good.price
    this.setData({
      number : count,
      money: mount,
      btnText: mount > 0 ? "加入购物车￥" + mount : "加入购物车"
    })
  },

  // 反馈给上个页面
  addCar(){
    const eventChannel = this.getOpenerEventChannel()
    eventChannel.emit('addGoods', {"count": this.data.number,"propertyValue": this.data.propertyValue});
    wx.navigateBack({
      delta: 1,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    this.setData({
      money: this.data.good.price * this.data.number
    })

    console.log(options)
    const eventChannel = this.getOpenerEventChannel()
    eventChannel.emit('acceptDataFromOpenedPage', {data: 'test'});
    eventChannel.emit('someEvent', {data: 'test'});
    // 监听acceptDataFromOpenerPage事件，获取上一页面通过eventChannel传送到当前页面的数据
    eventChannel.on('acceptDataFromOpenerPage', function(data) {
      console.log("addGoods页面接收的参数"+data)
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})